<?php

/**
 * uniBoard Style Templates
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date$
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_templates{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Style API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Style templates groups
	 *
	 * @var array
	 */
	
	private $groups			= array();
	
	/**
	 * Displayed templates
	 *
	 * @var array
	 */
	
	private $used_templates	= array();
	
	/**
	 * Templates from group
	 *
	 * @var array
	 */
	
	private $templates		= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Style API
		$this -> api = new api_style( $appCore, $appCore -> _REQ( 'style'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_style_not_found'], $appCore -> lang['acp_styles_list']);
			
			// Trow user back to groups list
			new action_styles( $appCore);	
		}
		else if ( $this -> api -> style_id == 1 && !DEVELOPER_MODE)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_style_master_templates'], $appCore -> lang['acp_styles_list']);
			
			// Trow user back to groups list
			new action_styles( $appCore);	
		}
		else if ( $appCore -> _REQ( 'do') == 'copy')
		{
			// Template to copy
			$new_tpl = new api_template( $appCore, $appCore -> _REQ( 'tpl'));
			
			// Not loaded or not ours?
			if ( !$new_tpl -> data_loaded || !in_array( $new_tpl -> style_id, $appCore -> getStyleParents( $this -> api -> style_id)) || $new_tpl -> style_id == $this -> api -> style_id)
			{
				// Bad Template
				$appCore -> output -> error( $appCore -> lang['acp_template_copy_incorrect'], $appCore -> lang['template_op_copy']);
							
				// Draw templates list
				$this -> drawTemplatesList();
			}
			else
			{
				// Copy Template
				$new_tpl -> setStyleId( $this -> api -> style_id);
				$new_tpl -> setTemplateGroup( $new_tpl -> template_group);
				$new_tpl -> setTemplateName( $new_tpl -> template_name);
				$new_tpl -> setTemplateCode( $new_tpl -> template_code);
				
				// Reset template id
				$new_tpl -> template_id = 0;
				
				// Errors?
				if ( $new_tpl -> error)
				{
					// Error in new Template
					$appCore -> output -> error( $new_tpl -> last_error, $appCore -> lang['template_op_copy']);
								
					// Draw templates list
					$this -> drawTemplatesList();
				}
				else
				{
					// Save template
					$new_tpl -> saveData();
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_copied_template', htmlspecialchars( $new_tpl -> style_name) . ' &raquo; ' . htmlspecialchars( $new_tpl -> template_name) . ' &raquo; ' . htmlspecialchars( $this -> api -> style_name));
				
					// Redirect
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id . '&show=' . urlencode( $new_tpl -> template_group)), $appCore -> lang['acp_template_copied']);
				}
			}
		}
		else
		{
			// Draw templates
			$this -> drawTemplatesList();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Templates list
	 *
	 */
	
	function drawTemplatesList(){
		
		// Start output buffer
		ob_start();
		
		// Add language string
		$this -> appCore -> output -> js_vars['l_delete_item_sure'] =  $this -> appCore -> lang['delete_item_sure'];
	
		// Open table
		echo $this -> appCore -> output -> openTableBlock( htmlspecialchars( $this -> api -> style_name));
		
		// Table header
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$this -> appCore -> lang['templates_list_template']
			),
			array(
				'right_float width_medium',
				$this -> appCore -> lang['actions']
			),
			array(
				'right_float width_medium',
				$this -> appCore -> lang['templates_list_size']
			)
		));
		
		// Select templates by id, group, name and lenght
		$this -> appCore -> db -> query( 'SELECT template_id, style_id, template_name, template_group, LENGTH(template_code) as template_size
		FROM ' . DB_PREFIX . 'styles_templates
		WHERE style_id IN (' . join( ',', $this -> appCore -> getStyleParents( $this -> api -> style_id)) . ')
		ORDER BY FIELD( style_id,' . join( ',', array_reverse( $this -> appCore -> getStyleParents( $this -> api -> style_id))) . ') DESC, template_name');
		
		// Style ID
		$style_id = $this -> api -> style_id;
		
		// Count templates
		while ( $tpl_result = $this -> appCore -> db -> fetch_assoc())
		{
			// Our group exists?
			if ( !isset( $this -> groups[$tpl_result['template_group']]))
			{
				$this -> groups[$tpl_result['template_group']] = array( 0, 0, 0);
			}
			
			// Add template?
			if ( !isset( $this -> used_templates[$tpl_result['template_group'] . ' - ' . $tpl_result['template_name']]))
			{
				// Set template
				$this -> used_templates[$tpl_result['template_group'] . ' - ' . $tpl_result['template_name']] = true;
				
				// Add to stats
				if ( $tpl_result['style_id'] == $style_id)
				{
					// Owned stat increase
					$this -> groups[$tpl_result['template_group']][2] ++;
				}
				else if ( $tpl_result['style_id'] == 1)
				{
					// Root stat increase
					$this -> groups[$tpl_result['template_group']][0] ++;
				}
				else
				{
					// Parent stat increase
					$this -> groups[$tpl_result['template_group']][1] ++;
				}
				
				// Display template?
				if ( $tpl_result['template_group'] == $this -> appCore -> _REQ( 'show'))
				{
					// Template in array?
					if ( !isset( $templates[$tpl_result['template_name']]))
					{
						// Template size
						$templates[$tpl_result['template_name']][0] = $tpl_result['template_id'];
						
						// Template owner
						if ( $tpl_result['style_id'] == $style_id)
						{
							// Root stat increase
							$templates[$tpl_result['template_name']][1] = 2;
						}
						else if ( $tpl_result['style_id'] == 1)
						{
							// Parent stat increase
							$templates[$tpl_result['template_name']][1] = 0;
						}
						else
						{
							// Owned stat increase
							$templates[$tpl_result['template_name']][1] = 1;
						}
						
						// Template size
						$templates[$tpl_result['template_name']][2] = $tpl_result['template_size'];
					}
					
				}
			}
		}
		
		// Draw templates groups?
		if ( !empty( $this -> groups))
		{
			foreach ( $this -> groups as $group_key => $group_stats)
			{
				// Draw spacer
				echo $this -> appCore -> output -> tableSpacer( array(
					array(
						'left_float width_quarter',
						( $group_key == $this -> appCore -> _REQ( 'show') ? '' : '<a href="' . $this -> appCore -> adminLink( 'act=templates&style=' . $style_id . '&show=' . urlencode( $group_key)) . '">') . htmlspecialchars( isset( $this -> appCore -> lang['tpl_group_' . $group_key]) ? $this -> appCore -> lang['tpl_group_' . $group_key] : $group_key) . ( $group_key == $this -> appCore -> _REQ( 'show') ? '' : '</a>')
					),
					array(
						'right_float width_medium',
						$this -> appCore -> output -> drawImage( 'style_owner_0', $this -> appCore -> lang['tpl_owner_0']) . ' ' . $group_stats[0] . ' ' . $this -> appCore -> output -> drawImage( 'style_owner_1', $this -> appCore -> lang['tpl_owner_1']) . ' ' . $group_stats[1] . ' ' . $this -> appCore -> output -> drawImage( 'style_owner_2', $this -> appCore -> lang['tpl_owner_2']) . ' ' . $group_stats[2]
					)
				));
				
				// Display group?
				if ( $group_key == $this -> appCore -> _REQ( 'show'))
				{
					// Reiterate templates
					foreach ( $templates as $template_name => $template_data)
					{
						if ( $template_data[1] == 2)
						{
							// Edit and delete actions
							$tpl_actions = '<a href="' . $this -> appCore -> adminLink( 'act=edit_template&do=edit&tpl=' . $template_data[0]) . '">' . $this -> appCore -> output -> drawImage( 'action_edit', $this -> appCore -> lang['edit']) . '</a>
							<a href="' . $this -> appCore -> adminLink( 'act=edit_template&do=delete&tpl=' . $template_data[0], true) . '" class="delete_confirm">' . $this -> appCore -> output -> drawImage( 'action_delete', $this -> appCore -> lang['delete']) . '</a>';
						}
						else
						{
							// Copy action
							$tpl_actions = '<a href="' . $this -> appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id . '&do=copy&tpl=' . $template_data[0]) . '">' . $this -> appCore -> output -> drawImage( 'action_copy', $this -> appCore -> lang['template_op_copy']) . '</a>';
						}
						
						// Draw row
						echo $this -> appCore -> output -> tableRow('
							<div class="cell left_float width_longer">
								' . $this -> appCore -> output -> drawImage( 'style_owner_' . $template_data[1], $this -> appCore -> lang['tpl_owner_' . $template_data[1]]) . ' <b>' . htmlspecialchars( $template_name) . '</b>
							</div>
							<div class="cell left_border right_float width_medium center">
								' . $tpl_actions . '
							</div>
							<div class="cell left_border right_float width_medium">
								' . class_strings::countSize( $template_data[2]) . '
							</div>');
					}
					
					// Garbage out
					unset( $tpl_actions);
				}
			}
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Draw New Template button
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_template&style=' . $style_id));
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['template_op_new']);
		echo $this -> appCore -> output -> closeForm();
		
		// Garbage out
		unset( $style_id);
		
		// Close table
		echo $this -> appCore -> output -> closeTableBlock();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_templates'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			$this -> appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id) => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
			
	}
		
}